package com.sesame.req;

import com.alibaba.fastjson.JSON;
import com.github.kevinsawicki.http.HttpRequest;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 几种网络请求的方式
 * 	<!-- 网络框架请求 -->
 * 		<!-- https://mvnrepository.com/artifact/com.github.kevinsawicki/http-request -->
 * 		<dependency>
 * 			<groupId>com.github.kevinsawicki</groupId>
 * 			<artifactId>http-request</artifactId>
 * 			<version>6.0</version>
 * 		</dependency>
 */
public class ReqTest {

    /**
     * 基础请求
     */
    static void baseReq() {
        String url = "";
        Map<String, String> params = new HashMap<>();
        String res = HttpRequest.post(url).form(params).body();
        System.out.println(res);
    }

    /**
     * json 请求
     */
    static void baseJsonReq() {
        String url = "";
        Map<String, String> params = new HashMap<>();

        String json = JSON.toJSONString(params);// json格式的参数
        String res = HttpRequest.post(url)
                .header("Content-Type", "application/json;charset=UTF-8")
//                .headers(getHeads(acc, name))
                .send(json).body();
        System.out.println(res);
    }

    /**
     * 携带文件流的请求
     */
    static void readFileReq() {
        String url = "";
        HttpRequest request = HttpRequest.post(url);
        try {
            MultipartFile file = null; // 文件数据,
            request.part("file"
                    , file.getOriginalFilename() // 文件名
                    , "multipart/form-data;"
                    , file.getInputStream() // 文件流
            );
        } catch (IOException e) {
            e.printStackTrace();
        }
        request.part("module", "参数的值");
        String res = request.body();
        System.out.println(res);
    }

    /**
     * 文件下载
     */
    static void fileDownload(){
        String pathstr = "xxx.xx"; // 文件地址
        File output = new File(pathstr);
        HttpRequest.get("下载地址").receive(output);
    }

    /**
     * https 忽略证书请求
     */
    static void httpsIgnoreCertificate() {
        String url = "";
        HttpRequest req = HttpRequest.post(url)
                .contentType("application/json", "UTF-8")
                .trustAllCerts().trustAllHosts();
//        HttpRequest req =  HttpRequest.post(sfVehicleCheckProperties.getVehicleUrl()).connectTimeout(10000).readTimeout(10000).contentType("application/json", "UTF-8").trustAllCerts().trustAllHosts();
        String res = req.send("json 参数").body();
        System.out.println(res);
    }


}
